package com.design_mode;

import java.util.Vector;

/**
 * Created by dustaple on 8/4/14.
 */
public class CompositePattern {
    public static void main(String[] args) {
        ICompany uGongHui = new UGongHui();

        IDepartment personelDepartment = new PersonnelDepartment();
        IDepartment techDepartment = new TechnologyDepartment();
        IDepartment softwareDepart = new SoftwareDevelopmentDepartment();

        IStaff gManager = new GeneralManager();
        IStaff hr = new HR();
        IStaff teo = new TEO();
        IStaff coder = new Coder();

        uGongHui.addDepartment(personelDepartment);
        uGongHui.addDepartment(techDepartment);
        uGongHui.addDepartment(softwareDepart);
        uGongHui.addStaff(gManager);

        personelDepartment.addStaff(hr);

        techDepartment.addDepartment(softwareDepart);
        techDepartment.addStaff(teo);

        softwareDepart.addStaff(coder);

        uGongHui.visitCompanyStructure();
    }
}

interface ICompany {
    void addDepartment(IDepartment department);

    void addStaff(IStaff staff);

    Vector<IDepartment> getDepartment();

    Vector<IStaff> getStaff();

    String getCompanyInfo();

    void visitCompanyStructure();

    void visitDepartment();

    void visitStaff();
}

interface IDepartment {
    void addDepartment(IDepartment department);

    void addStaff(IStaff staff);

    Vector<IDepartment> getDepartment();

    Vector<IStaff> getStaff();

    String getDepartmentInfo();

    void visitSubDepartment();

    void visitStaff();
}

interface IStaff {
    String getStaffInfo();
}

class UGongHui implements ICompany {
    Vector<IDepartment> departments = new Vector<IDepartment>();
    Vector<IStaff> staffs = new Vector<IStaff>();

    @Override
    public void addDepartment(IDepartment department) {
        departments.add(department);
    }

    @Override
    public void addStaff(IStaff staff) {
        staffs.add(staff);
    }

    @Override
    public Vector<IDepartment> getDepartment() {
        return departments;
    }

    @Override
    public Vector<IStaff> getStaff() {
        return staffs;
    }

    @Override
    public String getCompanyInfo() {
        System.out.println("一个软件公司");
        return "一个软件公司";
    }

    @Override
    public void visitCompanyStructure() {
        System.out.println(getCompanyInfo());
        visitStaff();
        visitDepartment();
    }

    @Override
    public void visitDepartment() {
        int length = departments.size();
        if (length > 0) {
            for (int i = 0; i < length; ++i) {
                departments.get(i).getDepartmentInfo();
                departments.get(i).visitStaff();
                departments.get(i).visitSubDepartment();
            }
        }
    }

    @Override
    public void visitStaff() {
        int length = staffs.size();
        if (length>0){
            for (int i=0; i<length; ++i){
                staffs.get(i).getStaffInfo();
            }
        }
    }
}

class PersonnelDepartment implements IDepartment {
    Vector<IDepartment> departments = new Vector<IDepartment>();
    Vector<IStaff> staffs = new Vector<IStaff>();

    @Override
    public void addDepartment(IDepartment department) {
        departments.add(department);
    }

    @Override
    public void addStaff(IStaff staff) {
        staffs.add(staff);
    }

    @Override
    public Vector<IDepartment> getDepartment() {
        return departments;
    }

    @Override
    public Vector<IStaff> getStaff() {
        return staffs;
    }

    @Override
    public String getDepartmentInfo() {
        System.out.println("人事部");
        return "人事部";
    }

    @Override
    public void visitSubDepartment() {
        int length = departments.size();
        if (length > 0) {
            for (int i = 0; i < length; ++i) {
                departments.get(i).getDepartmentInfo();
                departments.get(i).visitStaff();
                departments.get(i).visitSubDepartment();
            }
        }
    }

    @Override
    public void visitStaff() {
        int length = staffs.size();
        if (length>0){
            for (int i=0; i<length; ++i){
                staffs.get(i).getStaffInfo();
            }
        }
    }
}

class TechnologyDepartment implements IDepartment {
    Vector<IDepartment> departments = new Vector<IDepartment>();
    Vector<IStaff> staffs = new Vector<IStaff>();

    @Override
    public void addDepartment(IDepartment department) {
        departments.add(department);
    }

    @Override
    public void addStaff(IStaff staff) {
        staffs.add(staff);
    }

    @Override
    public Vector<IDepartment> getDepartment() {
        return departments;
    }

    @Override
    public Vector<IStaff> getStaff() {
        return staffs;
    }

    @Override
    public String getDepartmentInfo() {
        System.out.println("技术部");
        return "技术部";
    }

    @Override
    public void visitSubDepartment() {
        int length = departments.size();
        if (length > 0) {
            for (int i = 0; i < length; ++i) {
                departments.get(i).getDepartmentInfo();
                departments.get(i).visitStaff();
                departments.get(i).visitSubDepartment();
            }
        }
    }

    @Override
    public void visitStaff() {
        int length = staffs.size();
        if (length>0){
            for (int i=0; i<length; ++i){
                staffs.get(i).getStaffInfo();
            }
        }
    }
}

class SoftwareDevelopmentDepartment implements IDepartment {
    Vector<IDepartment> departments = new Vector<IDepartment>();
    Vector<IStaff> staffs = new Vector<IStaff>();

    @Override
    public void addDepartment(IDepartment department) {
        departments.add(department);
    }

    @Override
    public void addStaff(IStaff staff) {
        staffs.add(staff);
    }

    @Override
    public Vector<IDepartment> getDepartment() {
        return departments;
    }

    @Override
    public Vector<IStaff> getStaff() {
        return staffs;
    }

    @Override
    public String getDepartmentInfo() {
        System.out.println("软件开发部");
        return "软件开发部";
    }

    @Override
    public void visitSubDepartment() {
        int length = departments.size();
        if (length > 0) {
            for (int i = 0; i < length; ++i) {
                departments.get(i).getDepartmentInfo();
                departments.get(i).visitStaff();
                departments.get(i).visitSubDepartment();
            }
        }
    }

    @Override
    public void visitStaff() {
        int length = staffs.size();
        if (length>0){
            for (int i=0; i<length; ++i){
                staffs.get(i).getStaffInfo();
            }
        }
    }
}

class GeneralManager implements IStaff {
    @Override
    public String getStaffInfo() {
        System.out.println("公司总经理");
        return "公司总经理";
    }
}

class HR implements IStaff {
    @Override
    public String getStaffInfo() {
        System.out.println("人力资源");
        return "人力资源";
    }
}

class TEO implements IStaff {
    @Override
    public String getStaffInfo() {
        System.out.println("技术顾问");
        return "技术顾问";
    }
}

class Coder implements IStaff {
    @Override
    public String getStaffInfo() {
        System.out.println("程序员");
        return "程序员";
    }
}